package com.zs.grain.ui

import android.os.Bundle
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zs.grain.*
import com.xnkj1688.semplibrary.mvvm.*
import com.zs.grain.databinding.*

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
@Route(path = ARouterPath.FinishFragment)
class FinishFragment : BaseMvvmFragment<FragmentFinishBinding, FinishViewModel>() {
    override val layoutId = R.layout.fragment_finish

    override fun initViewModel(binding: FragmentFinishBinding) {
        binding.vm = this.mViewModel
        binding.view = this
    }

    override fun initCreate() {
        LiveEventBus.get("Finish", Bundle::class.java).observe(this, Observer {
            mViewModel.init(it.getLong("OrderId"))
        })
    }

    override fun initArguments() {
        var orderId = this.arguments?.getLong("OrderId")
        if (orderId != null) mViewModel.init(orderId)
    }

    override fun initAction() {
        super.initAction()
        LiveEventBus.get("DeviceService.Finish", Boolean::class.java)
            .observe(this, Observer {
                mViewModel.onFinish()
            })
        LiveEventBus.get("DeviceService.Completed", Boolean::class.java)
            .observe(this, Observer {
                mViewModel.onComplete()//TODO:由倒计时控制跳转
            })
    }

//    override fun onPause() {
//        super.onPause()
//        this.mViewModel.mSoundService.stopSound2()
//        //this.mViewModel.stopMPlayer()
//    }

    companion object {
        fun newInstance() = FinishFragment()
    }
}
